﻿
using Domain.User;
using MediatR;
using MES.Server.API.Application.Command.Account;
using MES.Server.API.Application.Command.Account.DocumentCodeRuleCommand;
using MES.Server.API.Dto;
using MES.Server.API.Dto.Account;
using MES.Server.API.Dto.Department;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using ReturnValue;
using System.Security.Claims;

namespace MES.Server.API.Controllers
{
    /// <summary>
    /// 账号控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountsController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IHttpContextAccessor httpContextAccessor;

        public AccountsController(IMediator mediator,IHttpContextAccessor httpContextAccessor)
        {
            this.mediator = mediator;
            this.httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>用户信息</returns>
        [HttpGet]
        public Task<ApiResult<LoginDTO>> Login([FromQuery] LoginCommand request)
        {
           
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="request">客户外键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>客户列表</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiPaging<OtherRoleDTO>> ClientGet([FromQuery] ClientGetCommand request)
        {
            //JWT载荷数据获取
             string OK = httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type== "AccountName").Value??"adminss";
            var str = User.FindFirstValue("AccountName");
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取用供应商列表
        /// </summary>
        /// <param name="request">用户外键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>用户列表</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiPaging<OtherRoleDTO>> SupplierGet([FromQuery] SupplierGetCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> ClientAdd(AccountAddCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 账号软删除
        /// </summary>
        /// <param name="request">主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> AccountDel(AccountDelCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 实现账号批量删除
        /// </summary>
        /// <param name="request">各个主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<ApiResult<int>> MoreDel(MoreDelCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 账号信息修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<ApiResult<int>> AccountUpdate(AccountUpdateCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="request">无参</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<DepartmentDTO>>> GetDepartments([FromQuery] GetDepartmentCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取角色下拉
        /// </summary>
        /// <param name="request">无参</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>角色列表</returns>
        [HttpGet]
        public Task<ApiResult<List<RoleDTO>>> RoleGet([FromQuery]RoleGetCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 创建编码规则
        /// </summary>
        [HttpPost]
        [ProducesResponseType(typeof(ApiResult<DocumentCodeRule>), 200)]
        public async Task<IActionResult> Create([FromBody] CreateDocumentCodeRuleCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 获取编码规则列表
        /// </summary>
        [HttpGet]
        [ProducesResponseType(typeof(ApiPaging<DocumentCodeRule>), 200)]
        public async Task<IActionResult> GetList([FromQuery] GetDocumentCodeRulesCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 更新编码规则
        /// </summary>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(ApiResult<DocumentCodeRule>), 200)]
        public async Task<IActionResult> Update(long id, [FromBody] UpdateDocumentCodeRuleCommand command)
        {
            command.Id = id;
            var result = await mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 删除编码规则
        /// </summary>
        [HttpDelete("{id}")]
        [ProducesResponseType(typeof(ApiResult<object>), 200)]
        public async Task<IActionResult> Delete(long id)
        {
            var command = new DeleteDocumentCodeRuleCommand { Id = id };
            var result = await mediator.Send(command);
            return Ok(result);
        }


        ///// <summary>
        ///// 创建单据编码规则 2025年9月19日19:29:36
        ///// </summary>
        //[HttpPost]
        //public async Task<ActionResult<ApiResult<DocumentCodeRuleDTO>>> Create([FromBody] CreateDocumentCodeRuleDTO dto)
        //{
        //    var command = new CreateDocumentCodeRuleCommand { Dto = dto };
        //    return Ok(await mediator.Send(command));
        //} 
        ///// <summary>
        ///// 查询单据编码规则列表
        ///// </summary>
        //[HttpGet]
        //public async Task<ActionResult<ApiResult<ApiPaging<DocumentCodeRuleDTO>>>> GetList(
        //    [FromQuery] int pageIndex = 1,
        //    [FromQuery] int pageSize = 20,
        //    [FromQuery] string businessEntity = "")
        //{
        //    var command = new GetDocumentCodeRuleListCommand
        //    {
        //        PageIndex = pageIndex,
        //        PageSize = pageSize,
        //        BusinessEntity = businessEntity
        //    };
        //    return Ok(await mediator.Send(command));
        //}

        ///// <summary>
        ///// 获取业务名称下拉框选项（图二业务名称下拉）
        ///// </summary>
        //[HttpGet]
        //public async Task<ActionResult<ApiResult<List<SelectListItem>>>> GetBusinessNameOptions()
        //{
        //    var command = new GetBusinessNameOptionsCommand();
        //    return Ok(await mediator.Send(command));
        //}

        ///// <summary>
        ///// 根据业务实体获取单据字段下拉框选项（图二单据字段下拉）
        ///// </summary>
        //[HttpGet]
        //public async Task<ActionResult<ApiResult<List<SelectListItem>>>> GetEntityFieldsByBusiness([FromQuery] string businessEntity)
        //{
        //    var command = new GetEntityFieldsByBusinessCommand { BusinessEntity = businessEntity };
        //    return Ok(await mediator.Send(command));
        //}
    }
}
